package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.bumptech.glide.Glide;
import com.tencent.mm.opensdk.utils.Log;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.QueryInterductionAreaRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryPageResourceRes;
import sz.szsmk.citizencard.model.network.clarkbean.response.AdInfo;
import sz.szsmk.citizencard.ui.fragment.AnnouncementFragment;
import sz.szsmk.citizencard.util.GlideRoundTransform;
import sz.szsmk.citizencard.widget.CustomRoundAngleImageView;
import sz.szsmk.citizencard.widget.MarqueeView;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/19 9:43
 * description:
 */
public class AppHomeBannerAdapter extends DelegateAdapter.Adapter<AppHomeBannerAdapter.AppHomeBannerHolder> implements OnBannerListener {
    private FragmentActivity mContext;
    private LayoutHelper mGridLayoutHelper;

    private ArrayList<String> titleList = new ArrayList<String>();

    private ArrayList<Fragment> mFragment;
    private AnnouncementFragment announcementFragment;
    private TabFragmentPagerAdapter adapter;
    private ArrayList<String> iconList = new ArrayList<>();
    private ArrayList<String> stringList = new ArrayList<>();
    private List<QueryPageResourceRes.ResourceListBean> banner = new ArrayList<>();


    private BaseInterface baseInterface;
    private List<QueryInterductionAreaRes.InterdutionListBean> viewData;
    private int showView;

    ApphomeBanner adadapter;

    public void setViewData(List<QueryInterductionAreaRes.InterdutionListBean> viewData) {
        this.viewData = viewData;
        Log.d("clark", viewData.size() + viewData.get(0).getTitle());
    }


    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header, QueryPageResourceRes.ResourceListBean resourceListBean);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }


    public AppHomeBannerAdapter(FragmentActivity activity, DefaultLayoutHelper headHelper, int showView) {
        mContext = activity;
        mGridLayoutHelper = headHelper;
        this.showView = showView;

    }


    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public AppHomeBannerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.apphome_banner_view, parent, false);
        return new AppHomeBannerHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.apphome_banner_view;
    }


    public static ArrayList<String> removeDuplicate(ArrayList<String> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    @Override
    public void onBindViewHolder(AppHomeBannerHolder holder, int position) {


        if (viewData != null) {

            for (int i = 0; i < viewData.size(); i++) {
                String content = viewData.get(i).getContent();
                stringList.add(content);
            }

            removeDuplicate(stringList);
            if (showView == 0) {
                holder.ll_gonggao.setVisibility(View.VISIBLE);
                holder.ll_banner_banner.setVisibility(View.GONE);
            }

            if (showView == 1) {
                holder.ll_gonggao.setVisibility(View.GONE);
                holder.ll_banner_banner.setVisibility(View.VISIBLE);
            }


            holder.ll_gonggao.setOnClickListener(v -> {
                int mNendHeader = 0;
                int positionClick = holder.mMarqueeView.getPosition();
                QueryInterductionAreaRes.InterdutionListBean interdutionListBean = viewData.get(positionClick);
                String link_url = interdutionListBean.getLink_url();
                String need_header = interdutionListBean.getNeed_header();
                if (need_header != null) {
                    mNendHeader = Integer.valueOf(need_header).intValue();
                }
                String sys_label = interdutionListBean.getSys_label();

                QueryPageResourceRes.ResourceListBean resourceListBean = new QueryPageResourceRes.ResourceListBean();
                resourceListBean.setModularId(interdutionListBean.getModularId());
                baseInterface.onClick(holder.ll_gonggao, link_url, sys_label, mNendHeader, resourceListBean);
            });
            holder.mMarqueeView.startWithList(stringList);
            holder.mMarqueeView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {

                private int mNendHeader;
                private QueryInterductionAreaRes.InterdutionListBean interdutionListBean;

                @Override
                public void onItemClick(int position, TextView textView) {
                    if (viewData != null) {
                        interdutionListBean = viewData.get(position);
                        String link_url = interdutionListBean.getLink_url();
                        String need_header = interdutionListBean.getNeed_header();
                        if (need_header != null) {
                            mNendHeader = Integer.valueOf(need_header).intValue();
                        }
                        String sys_label = interdutionListBean.getSys_label();

                        QueryPageResourceRes.ResourceListBean resourceListBean = new QueryPageResourceRes.ResourceListBean();
                        resourceListBean.setModularId(interdutionListBean.getModularId());
                        baseInterface.onClick(textView, link_url, sys_label, mNendHeader, resourceListBean);

                    }
                }
            });
        }


        if (holder.banner.getAdapter() == null) {
            holder.banner.setAdapter(adadapter = new ApphomeBanner(banner));
        }
        holder.banner.getAdapter().setDatas(banner);
        holder.banner.getAdapter().notifyDataSetChanged();
        holder.banner.setOnBannerListener(this);
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    @Override
    public void OnBannerClick(Object data, int position) {
        QueryPageResourceRes.ResourceListBean resourceListBean = banner.get(position);
        if (resourceListBean != null) {

            String link_url = resourceListBean.getLink_url();
            int need_header = resourceListBean.getNeed_header();
            String sys_label = resourceListBean.getSys_label();
            if (baseInterface != null) {
                baseInterface.onClick(null, link_url, sys_label, need_header, resourceListBean);
            }
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void setBanner(List<AdInfo> banner) {
        this.banner.clear();

        for (int i = 0; i < banner.size(); i++) {
            QueryPageResourceRes.ResourceListBean bean = new QueryPageResourceRes.ResourceListBean();
            bean.setLink_url(banner.get(i).getLink_url());
            bean.setResource_url(banner.get(i).getResource_url());
            bean.setModularId(banner.get(i).getResource_area_id());
            bean.setRank(banner.get(i).getRank());
            Log.d("clark", banner.get(i).getResource_url());

            this.banner.add(bean);
        }

        banner.sort((x, y) -> Integer.compare(x.getRank(), y.getRank()));


//        this.banner.addAll(banner);
    }


    public static class AppHomeBannerHolder extends RecyclerView.ViewHolder {


        private final ViewPager mainVp;
        private final Banner banner;
        private final MarqueeView mMarqueeView;
        private final LinearLayout ll_gonggao;
        private final LinearLayout ll_banner_banner;

        public AppHomeBannerHolder(View itemView) {
            super(itemView);
            mainVp = (ViewPager) itemView.findViewById(R.id.main_vp);
            mMarqueeView = (MarqueeView) itemView.findViewById(R.id.apphome_banner_vt);
            banner = (Banner) itemView.findViewById(R.id.banner);
            ll_gonggao = itemView.findViewById(R.id.ll_gonggao);
            ll_banner_banner = itemView.findViewById(R.id.layout_banner_banner);
        }
    }

    public class ApphomeBanner extends com.youth.banner.adapter.BannerAdapter<QueryPageResourceRes.ResourceListBean, ApphomeBanner.BannerViewHolder> {


        public ApphomeBanner(List<QueryPageResourceRes.ResourceListBean> datas) {
            super(datas);
        }

        @Override
        public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            ImageView imageView = new ImageView(parent.getContext());
            //注意，必须设置为match_parent，这个是viewpager2强制要求的
            imageView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return new BannerViewHolder(parent.getContext(), imageView, imageView);
        }

        @Override
        public void onBindView(BannerViewHolder holder, QueryPageResourceRes.ResourceListBean data, int position, int size) {
            Glide.with(mContext).load(data.getResource_url())
                    .into(holder.iv);
        }

        class BannerViewHolder extends ViewHolder {

            ImageView iv;

            public BannerViewHolder(Context context, View itemView, ImageView iv) {
                super(context, itemView);
                this.iv = iv;
            }
        }
    }


}
